Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
http-terminator
Advanced tools
The http-terminator npm package provides a reliable way to gracefully terminate HTTP and HTTPS servers. It ensures that all active connections are properly closed before the server shuts down, preventing potential data loss or corruption.
Graceful Server Termination
This feature allows you to gracefully terminate an HTTP server, ensuring that all active connections are properly closed before the server shuts down.
const { createHttpTerminator } = require('http-terminator');
const http = require('http');
const server = http.createServer((req, res) => {
res.end('Hello, world!');
});
server.listen(3000);
const httpTerminator = createHttpTerminator({ server });
// To terminate the server gracefully
httpTerminator.terminate().then(() => {
console.log('Server terminated');
});
Handling Long-Lived Connections
This feature demonstrates how http-terminator handles long-lived connections, ensuring they are properly closed before the server shuts down.
const { createHttpTerminator } = require('http-terminator');
const http = require('http');
const server = http.createServer((req, res) => {
setTimeout(() => {
res.end('Delayed response');
}, 10000); // Simulate a long-lived connection
});
server.listen(3000);
const httpTerminator = createHttpTerminator({ server });
// To terminate the server gracefully
httpTerminator.terminate().then(() => {
console.log('Server terminated');
});
Custom Termination Timeout
This feature allows you to set a custom timeout for graceful termination, giving you control over how long the server should wait before forcefully closing connections.
const { createHttpTerminator } = require('http-terminator');
const http = require('http');
const server = http.createServer((req, res) => {
res.end('Hello, world!');
});
server.listen(3000);
const httpTerminator = createHttpTerminator({
server,
gracefulTerminationTimeout: 5000 // Custom timeout in milliseconds
});
// To terminate the server gracefully
httpTerminator.terminate().then(() => {
console.log('Server terminated');
});
The stoppable package provides similar functionality by allowing you to gracefully stop an HTTP server. It ensures that existing connections are properly closed before the server shuts down. Compared to http-terminator, stoppable is simpler but may lack some advanced features and configurability.
The http-shutdown package extends the Node.js HTTP server with a graceful shutdown capability. It ensures that all active connections are closed before the server shuts down. While it offers similar functionality to http-terminator, it may not be as actively maintained or feature-rich.
The graceful-server package provides a way to gracefully shut down HTTP servers, ensuring that all active connections are properly closed. It offers similar functionality to http-terminator but may have different configuration options and usage patterns.
Gracefully terminates HTTP(S) server.
When you call server.close()
, it stops the server from accepting new connections, but it keeps the existing connections open indefinitely. This can result in your server hanging indefinitely due to keep-alive connections or because of the ongoing requests that do not produce a response. Therefore, in order to close the server, you must track creation of all connections and terminate them yourself.
http-terminator implements the logic for tracking all connections and their termination upon a timeout. http-terminator also ensures graceful communication of the server intention to shutdown to any clients that are currently receiving response from this server.
import {
createHttpTerminator,
} from 'http-terminator';
/**
* @property gracefulTerminationTimeout Number of milliseconds to allow for the active sockets to complete serving the response (default: 5000).
* @property server Instance of http.Server.
*/
type HttpTerminatorConfigurationInputType = {|
+gracefulTerminationTimeout?: number,
+server: Server,
|};
/**
* @property terminate Terminates HTTP server.
*/
type HttpTerminatorType = {|
+terminate: () => Promise<void>,
|};
const httpTerminator: HttpTerminatorType = createHttpTerminator(
configuration: HttpTerminatorConfigurationInputType
);
Use createHttpTerminator
to create an instance of http-terminator and instead of using server.close()
, use httpTerminator.terminate()
, e.g.
import http from 'http';
import {
createHttpTerminator,
} from 'http-terminator';
const server = http.createServer();
const httpTerminator = createHttpTerminator({
server,
});
await httpTerminator.terminate();
Usage with Express example:
import express from 'express';
import {
createHttpTerminator,
} from 'http-terminator';
const app = express();
const server = app.listen();
const httpTerminator = createHttpTerminator({
server,
});
await httpTerminator.terminate();
Usage with Fastify example:
import fastify from 'fastify';
import {
createHttpTerminator,
} from 'http-terminator';
const app = fastify();
void app.listen(0);
const httpTerminator = createHttpTerminator({
server: app.server,
});
await httpTerminator.terminate();
Usage with Koa example:
import Koa from 'koa';
import {
createHttpTerminator,
} from 'http-terminator';
const app = new Koa();
const server = app.listen();
const httpTerminator = createHttpTerminator({
server,
});
await httpTerminator.terminate();
As it should be clear from the usage examples for Node.js HTTP server, Express and Koa, http-terminator works by accessing an instance of a Node.js http.Server
. To understand how to use http-terminator with your framework, identify how to access an instance of http.Server
and use it to create a http-terminator instance.
There are several alternative libraries that implement comparable functionality, e.g.
The main benefit of http-terminator is that:
connection: close
headerTo gracefully terminate a HTTP server.
We say that a service is gracefully terminated when service stops accepting new clients, but allows time to complete the existing requests.
There are several reasons to terminate services gracefully:
FAQs
Gracefully terminates HTTP(S) server.
The npm package http-terminator receives a total of 638,019 weekly downloads. As such, http-terminator popularity was classified as popular.
We found that http-terminator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.